/**
 * StarListGUI
 * 
 * @author
 * 
 */
package a3part2;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;

public class StarMenuGUI extends JFrame implements ActionListener {
	/**
	 * Data members
	 */
	private static final long serialVersionUID = 1L;
	private final int FRAME_X=100, FRAME_Y=100, FRAME_WIDTH=500,  FRAME_HEIGHT=400;
	private final int SCROLL_X=30, SCROLL_Y=30, SCROLL_WIDTH=300, SCROLL_HEIGHT=200;
	private final int SORT_X=30,   SORT_Y=235,  SORT_WIDTH=100,   SORT_HEIGHT=30;
	private String SORT_STRING = "Sort";
	private String SHUFFLE_STRING = "Shuffle";
	private JList<String> listview;
	private JScrollPane scroll;
	
	private JButton sort;
	private JMenuBar menuBar;
	private JMenu    fileMenu;
	private JMenu    editMenu;
	private JMenuItem openStarFile;
	private JFileChooser fileChooser;
	

	/**
	 * StarListGUI constructor
	 * 
	 * @param title
	 *
	 */
	public StarMenuGUI(String title) {
		super(title);
		setBounds(FRAME_X, FRAME_Y, FRAME_WIDTH, FRAME_HEIGHT);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		setLayout( null );
		
		/*
		 * Data that will be used as the "List Model"
		 */
		String[] stars = {
				"Polaris Australis", "Pollux", "Rana", "Sirius", "Yed Prior",
				"Electra", "Gacrux", "Gianfar", 
				"Maia", "Merga", "Nash", "Nodus Secundus", 
				"Peacock", "Pherkad Minor", "Pleione", 
				"Gienah Cygni", "Grafias", "Head of Hydrus",
				"Herschel's Garnet Star", "Hyadum II", "Kaus Borealis"
		};
		DefaultListModel<String> listmodel = new DefaultListModel<String>();
		for (String s : stars)
			listmodel.addElement( s );

		/*
		 * Now that the data is available, set up GUI elements to display it.
		 */
		listview = new JList<String>( listmodel );
		listview.setVisible(true);
		scroll = new JScrollPane( listview );
		scroll.setBounds(SCROLL_X, SCROLL_Y, SCROLL_WIDTH, SCROLL_HEIGHT);
		add( scroll );
		
		sort = new JButton( SORT_STRING );
		sort.addActionListener( this );
		sort.setBounds(SORT_X, SORT_Y, SORT_WIDTH, SORT_HEIGHT);
		add(sort);

		
		/*
		 * Set up the menus
		 */
		menuBar = new JMenuBar();
		fileMenu = new JMenu("File");
		editMenu = new JMenu("Edit");
		menuBar.add( fileMenu );
		menuBar.add( editMenu );
		openStarFile = new JMenuItem("Open Star File");
		openStarFile.addActionListener( this );
		fileMenu.add( openStarFile );
		setJMenuBar( menuBar );
		fileChooser = new JFileChooser();
		
		setVisible( true );
	}

	/**
	 * actionPerformed - 
	 * 
	 * 
	 */
	public void actionPerformed(ActionEvent ae) {
		Object source = ae.getSource();
		if (source.equals(sort)){
			if (sort.getText().equals( SORT_STRING )){
				sort.setText(SHUFFLE_STRING);
				/*
				 * Sort
				 */

			} else {
				if (sort.getText().equals( SHUFFLE_STRING )){
					sort.setText( SORT_STRING );
				/*
				 * Shuffle
				 */

				}
				
			}
		} else if (source.equals(openStarFile)){
			int returnValue = fileChooser.showOpenDialog( this );
			System.out.println("File chooser returned: " + returnValue );
		}
	}

	/**
	 * main method
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		new StarMenuGUI("My Star System");
	}
}
